home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8822 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.2 KB  |  50 lines

  1. Path: nntp.teleport.com!nix
  2. From: nix@teleport.com (Jonathan Nix)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Need help with strings
  5. Date: 26 Feb 1996 21:13:44 GMT
  6. Organization: Teleport - Portland's Public Access (503) 220-1016
  7. Message-ID: <4gt7q8$co3@maureen.teleport.com>
  8. References: <312fa506.21210161@news.ucla.edu> <4gpf9b$r7g@news1.usa.pipeline.com>
  9. NNTP-Posting-Host: linda.teleport.com
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. : >OK, I just figured out what I did.  I had a constructor which was 
  13. : >setting all the pointers to equal 0.  I thought that this would 
  14. : >initialize them to point to null but apparently it won't let me assign 
  15. : >anything to the variable later. 
  16. :  
  17. : Phil:  I'm afraid you've figured it wrong -- or you're not saying what 
  18. : you mean -- or I misunderstand.   
  19. :  
  20. : First, initializing pointers to 0 does make them to point to what  
  21. : most programmers consider NULL. 
  22. :  
  23. : Second, initializing variables to anything, including 0, does not 
  24. : preclude you from assigning to those variables later.  It does, 
  25. : however, mean that you can't strcpy to/from those variables 
  26. : until they have been rebound to point to some valid memory 
  27. : block.  But that's different. 
  28.  
  29.     A character pointer is just a character pointer, by adding a 
  30. value to the address in increments of characters you get the array of 
  31. characters, or string of characters. If you initialize the character 
  32. pointer to zero you get a character pointer reading ascii 0 when 
  33. dereferenced. Either way, the memory above the pointer is not allocated 
  34. for use unless you specificaly allocate it with the special character 
  35. array initialization syntax..
  36.  
  37.     char *ch = "String of fixed length.\0";
  38.  
  39.     To specify a runtime determined length you use malloc, and to specify 
  40. a compile time constant length you use the array syntax (char ch[10];). 
  41. Either way you cannot run past the length you specified for the string 
  42. without retrieving either a NPA or SIGSEGV error. 
  43.  
  44.     There is no built in dynamic input of a character array in any ANSI-C++ 
  45. press release that I am aware of. However you can easily write a function 
  46. to return a string of unknown length, by using memory buffers of fixed 
  47. length in the function.
  48.  
  49.         Jonathan Nix
  50.